package com.shop2cn.iapi.sqdistributor.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.SetParentDistributorReq;
import com.shop2cn.iapi.sqdistributor.service.SetParentDistributorCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_SetParentDistributor {
	
	//java接口
	private static SetParentDistributorReq setParentDistributorReq;
	private static SetParentDistributorCall setParentDistributorCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置分销员上级");
	}

	@Before
	public void caseUp() {
		setParentDistributorReq = new SetParentDistributorReq();
		setParentDistributorCall = new SetParentDistributorCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setParentDistributorCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetParentDistributor_001() {
		Logger.start(true, "设置分销员上级成功");
		
		try {
			
			DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();
			
			Long merchantId = (long)TestDataManager.getRandomIntNum(11);
			Long id = SqDistributorService.auditDistributorApplySuccess(merchantId);
			
			int oldParent_beforeUpdateCount = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(500861049L, 34569584L)
					.getDirect_subordinate_count();
			int newParent_beforeUpdateCount = distributorRelationWrapper
					.selectByPrimaryKey(32L).getDirect_subordinate_count();
			
			setParentDistributorReq.setId(id);
			setParentDistributorReq.setParentDistributorId(1105877737L);
			
			setParentDistributorCall.setData(setParentDistributorReq);
			setParentDistributorCall.callService();
			
			Logger.verifyEquals(true, setParentDistributorCall.getSuccess(), "验证success");
			
			DistributorRelation actualResult = distributorRelationWrapper
					.selectByPrimaryKey(setParentDistributorReq.getId());
			int oldParent_afterUpdateCount = distributorRelationWrapper
					.selectByMerchantIdAndDistributorId(500861049L, 34569584L)
					.getDirect_subordinate_count();
			int newParent_afterUpdateCount = distributorRelationWrapper
					.selectByPrimaryKey(32L).getDirect_subordinate_count();

			Logger.verifyEquals(
					setParentDistributorReq.getParentDistributorId(),
					actualResult.getParent_distributor_id(),
					"验证Parent_distributor_id");
			Logger.verifyEquals(distributorRelationWrapper
					.selectByDistributorId(1105877737L).getDistributor_name(),
					actualResult.getParent_distributor_name(),
					"验证Parent_distributor_name");
			Logger.verifyEquals(oldParent_beforeUpdateCount - 1,
					oldParent_afterUpdateCount, "验证旧上级的直属下级数量");
			Logger.verifyEquals(newParent_beforeUpdateCount + 1,
					newParent_afterUpdateCount, "验证新上级的直属下级数量");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetParentDistributor_002() {
		Logger.start(true, "id为null，设置分销员上级失败");
		
		try {
			
			//setParentDistributorReq.setId(8L);
			setParentDistributorReq.setParentDistributorId(33L);
			
			setParentDistributorCall.setData(setParentDistributorReq);
			setParentDistributorCall.callService();
			
			Logger.verifyEquals(false, setParentDistributorCall.getSuccess(), "验证success");
			Logger.verifyEquals("id:must be greater than or equal to 1", setParentDistributorCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetParentDistributor_003() {
		Logger.start(true, "ParentDistributorId为null，设置分销员上级失败");
		
		try {
			
			setParentDistributorReq.setId(8L);
			//setParentDistributorReq.setParentDistributorId(33L);
			
			setParentDistributorCall.setData(setParentDistributorReq);
			setParentDistributorCall.callService();
			
			Logger.verifyEquals(false, setParentDistributorCall.getSuccess(), "验证success");
			Logger.verifyEquals("parentDistributorId:must be greater than or equal to 1", setParentDistributorCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
